﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MOJsdk;
using System.Collections.ObjectModel;
using Microsoft.Win32;

namespace MOJ
{
    /// <summary>
    /// Interaction logic for Control_SelectFile.xaml
    /// </summary>
    public partial class Control_SelectFile : UserControl
    {
        public Control_SelectFile()
        {
            InitializeComponent();
            INI();
        }

        #region Inicialization
        private void INI()
        {
            INIEvents();
            INIStyles();
        }

        private void INIStyles()
        {
            _btn.Content = MOJLocalization.Localize("Browse Files");

            _listView.SelectionMode = SelectionMode.Single;

            _Grid.ApplyStyle("0101098");
            _listView.ApplyStyle("0101099");
            _btn.ApplyStyle("0101100");

        }
        private void INIEvents()
        {
            OnFileSelected += delegate { };
            OnSearchFile += delegate { };

            this.Loaded += ThisLoaded;

            _btn.Click += SearchFile;
            _listView.SelectionChanged += SelectFile;
        }
        #endregion

        private ObservableCollection<File2Add> _files;

        private void ThisLoaded(object sender, RoutedEventArgs e)
        {
            _files = new ObservableCollection<File2Add>(((Control_HandleFiles)GlobalVars.TheHost.GetElement("MOJ_managefiles")).FilesToAdd);
            _listView.ItemsSource = _files;
        }
        private void SearchFile(object sender, RoutedEventArgs e)
        {
            OnSearchFile(sender, e);
        }
        private void SelectFile(object sender, RoutedEventArgs e)
        {
            int x = _listView.SelectedIndex;
            if (x == -1) return;

            OnFileSelected(_files[x], x);
        }

        public delegate void FileSelected(File2Add File, int Index);
        public event FileSelected OnFileSelected;
        public event RoutedEventHandler OnSearchFile;
    }
}
